# Author: Chris Moyer
# Basic user class
from marajo.appengine.ext import db
import smtplib
import marajo

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText


class User(db.Model):
    """
    Basic authentication user
    information is stored in the DB
    """
    username = db.StringProperty(verbose_name="Username")
    nickname = db.StringProperty(verbose_name="Name")
    email = db.StringProperty(verbose_name="Email Address")
    auth_groups = db.ListProperty(str, verbose_name="Authorization Groups")
    oids = db.ListProperty(str, verbose_name="OpenID Identifiers")
    password = db.PasswordProperty()

    def __str__(self):
        return self.nickname

    def has_auth_group(self, group):
        return (group in self.get_auth_groups())
    
    def get_auth_groups(self):
        return self.auth_groups

    def add_auth_group(self, group):
        """
        Add authorization group, only if it doesn't already have it
        """
        if not group in self.auth_groups:
            self.auth_groups.append(group)
            self.put()

    def remove_auth_group(self, group):
        if group in self.auth_groups:
            self.auth_groups.remove(group)
            self.put()

    def notify(self, subject, body='', html_body=None, attachments=[]):
        """
        Send notification to this user

        @param subject: Subject for this notification
        @type subject: str

        @param body: The message to send you
        @type body: str
        """
        import boto.utils
        boto.utils.notify(subject, body=body, 
                                html_body=html_body, 
                                attachments=attachments, 
                                to_string=self.email,
                                append_instance_id=False)
